package com.techsoul.module.basic.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.log.annotation.Log;
import com.techsoul.common.log.enums.OperationTypeEnum;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.basic.pojo.param.StoreSearchParam;
import com.techsoul.module.basic.pojo.param.SupplierSaveParam;
import com.techsoul.module.basic.pojo.param.SupplierSearchParam;
import com.techsoul.module.basic.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 供应商 Controller
 *
 * @author liuen
 * @date 2021-01-26
 */
@Api(tags = "供应商")
@RestController
@RequestMapping("/basic/supplier")
public class SupplierController {

    @Autowired
    private Result result;
    
    @Autowired
    private ISupplierService service;

    @Autowired
    private TokenService tokenService;
    @ApiOperation("新增")
    @Log(title = "供应商", operationType = OperationTypeEnum.INSERT)
    @PreAuthorize("hasAnyAuthority('basic:supplier:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody SupplierSaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @Log(title = "供应商", operationType = OperationTypeEnum.DELETE)
    @PreAuthorize("hasAnyAuthority('basic:supplier:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("相关供应商详情")
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/materialSupplierList/{id}")
    public Result materialSupplierList(@PathVariable("id") String id) {
        return result.success(this.service.materialSupplierList(id));
    }



    @ApiOperation("企业总数-按区域查询")
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/findTotalCompany")
    public Result findTotalCompany() {
        UserVo sysuser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        return result.success(this.service.findTotalCompany(sysuser.getSite()));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @Log(title = "店铺", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('basic:supplier:delete')")
    @PostMapping("/logicalDelete/{ids}")
    public Result logicalDelete(@PathVariable String[] ids) {
        return result.success(this.service.logicalDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @Log(title = "供应商", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('basic:supplier:update')")
    @PutMapping
    public Result update(@Valid @RequestBody SupplierSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("修复数据")
    @PreAuthorize("hasAnyAuthority('basic:supplier:update')")
    @PostMapping("/fixdata")
    public Result fixdata(@RequestBody SupplierSaveParam params) {
        this.service.fixdata();
        return result.success();
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody SupplierSearchParam params) {
        return result.success(this.service.findAll(params));
    }
    @ApiOperation("列表")
    //@PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/mylist")
    public Result mylist(@RequestBody SupplierSearchParam params) {
        UserVo sysuser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(StringUtils.isNull(sysuser.getCustomerId())){
            params.setCustomerNo(sysuser.getSite());
        }else{
            params.setCustomerNo(sysuser.getCustomerId());
        }

        return result.success(this.service.findAll(params));
    }

    @ApiOperation("校验供应商编码")
    @Log(title = "校验供应商编码", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/checkCode")
    public Result checkCode(@Valid @RequestBody SupplierSearchParam params) {
        return result.success(this.service.checkCode(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/contractExppage/{current}/{pageSize}")
    public Result findContractExppage(@RequestBody SupplierSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findContractExppage(params, current, pageSize));
    }


    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('basic:supplier:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody SupplierSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

}
